//
//  WmcCreateTeamVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/15.
//

import RxSwift
import SVProgressHUD
import UIKit

// MARK: - 扩展WmcCreateTeamVC + WmcNavigatorProtocol

extension WmcCreateTeamVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.createTeamAlbum]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vc = UIViewController()
        if let vm = parms.context as? WmcCreateTeamVM {
            vc = WmcCreateTeamVC(vm)
        }
        return vc
    }
}

// MARK: - 扩展WmcCreateTeamVC + WmcVMProtocol

extension WmcCreateTeamVC: WmcVMProtocol {
    typealias VMType = WmcCreateTeamVM
}

// MARK: - [WmcCreateTeamVC]

class WmcCreateTeamVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "团队名称"
        createButton.title = viewModel.opType.text
        /// 默认弹出键盘
        self.specifiInputView.inputTextfield.becomeFirstResponder()
    }

    // Internal

    lazy var specifiInputView: WmcSpecificInputItemView = {
        let view = WmcSpecificInputItemView()
        view.placeholder = "输入团队名称"
        view.inputTextfield.text = viewModel.teamAblum?.name
        return view
    }()

    lazy var noteLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH8_8A8A99
        view.font = 12.yp.uiFont
        view.text = "团队名称最多15个字"
        return view
    }()

    lazy var createButton: WmcNormalButton = {
        let view = WmcNormalButton()
        view.backgroundColor = UIColor.wmc_mainTheme
        view.font = 16.yp.uiFont(weight: .semibold)
        view.textColor = UIColor.wmc_white_FFFFFF
        view.title = "创建"
        view.textAlignment = .center
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()

    override func makeUI() {
        super.makeUI()

        makeNavView(showShadow: false)

        view.addSubview(specifiInputView)
        view.addSubview(noteLabel)
        view.addSubview(createButton)

        specifiInputView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(16)
            make.left.right.equalToSuperview()
        }

        noteLabel.snp.makeConstraints { make in
            make.top.equalTo(specifiInputView.snp.bottom).offset(8)
            make.left.equalTo(16)
            make.right.equalTo(specifiInputView.snp.right)
        }

        createButton.snp.makeConstraints { make in
            make.top.equalTo(specifiInputView.snp.bottom).offset(101)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(44)
        }
    }

    override func bindViewModel() {
        /// 限制输入字符个数
        specifiInputView.inputTextfield.limitInputCharactersCount(count: 15)

        specifiInputView.inputTextfield.rx.text.map { ($0 ?? "").count > 0 }.bind(to: self.createButton.rx.isEnabled).disposed(by: rx.disposeBag)

        specifiInputView.inputTextfield.rx.text.map(transform(text:)).bind(to: self.createButton.rx.backgroundColor).disposed(by: rx.disposeBag)
    }

    override func bindViewAction() {
        if self.viewModel.opType == .create {
            /// 创建团队
            createButton.yp.addEvent { [weak self] _ in
                guard let weakSelf = self else { return }
                weakSelf.viewModel.createTeam(teamName: weakSelf.specifiInputView.inputTextfield.text ?? "") { resp in
                    weakSelf.viewModel.findCurrentSpecificTeam(teamId: resp.teamId ?? 0) { teamAlbum in
                        /// 创建成功跳转首页
                        let teamAlbumModel = WmcMediaTeamAlbumModel()
                        teamAlbumModel.teamId = resp.teamId ?? 0
                        teamAlbumModel.busId = "\(resp.teamId ?? 0)"
                        teamAlbumModel.albumId = resp.albumId ?? 0
                        teamAlbumModel.teamOpenId = teamAlbum.teamOpenId
                        teamAlbumModel.name = weakSelf.specifiInputView.inputTextfield.text ?? ""
                        if weakSelf.viewModel.opType == .create {
                            weakSelf.navigationController?.viewControllers.removeLast()
                        }
                        // update红点
                        weakSelf.viewModel.updateAblumInfo(teamAlbumModel)
                        WmcNavigatorManager.push(.teamCloudAlbum, parms: teamAlbumModel)
                    }
                }
            }
        }

        if self.viewModel.opType == .modify {
            /// 修改团队名称
            createButton.rx.controlEvent(.touchUpInside).debounce(.seconds(Int(0.5)), scheduler: MainScheduler.instance).bind { [weak self] _ in
                guard let weakSelf = self else { return }
                WmcHudManager.loading()
                let teamName = weakSelf.specifiInputView.inputTextfield.text ?? ""
                weakSelf.viewModel.updateTeamName(teamName: teamName) {
                    WmcNotificationManager.shared.post(type: .editTeamNameSucceed, object: ["teamName": teamName])
                    weakSelf.popVC()
                    WmcHudManager.dismiss()
                }
            }.disposed(by: rx.disposeBag)
        }
    }

    func transform(text: String?) -> UIColor {
        if text == nil {
            return UIColor.wmc_grayH14_CCCCCC
        } else {
            if let t = text, t.count > 0 {
                return UIColor.wmc_mainTheme
            } else {
                return UIColor.wmc_grayH14_CCCCCC
            }
        }
    }
}
